package com.china08.yxyapi.api.manage.tec.teaching.lesson.topic;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsTopicLessonItem;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsVideo;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyLessonsSchoolLessonRepository;
import com.china08.yxyapi.repository.db.YxyLessonsTopicLessonItemRepository;
import com.china08.yxyapi.repository.db.YxyLessonsVideoRepository;
import com.china08.yxyapi.util.YxyKeChengUtil;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class YxyLessonsTopicDetailItemController extends YxyApiBaseController {

    @Autowired
    YxyLessonsTopicLessonItemRepository itemRepository;

    @Autowired
    YxyLessonsSchoolLessonRepository lessonRepository;

    @Autowired
    YxyLessonsVideoRepository videoRepository;

    @Autowired
    YxyKeChengUtil YxyKeChengUtil;

    @ApiOperation("")
    @RequestMapping(value = "/api_yw/manage/tec/teaching/topic/item/detail", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        String itemId = reqModel.itemId;
        String userId = getProfile(request, response).getId();
        boolean userVip = Boolean.parseBoolean(getProfile(request, response).getUserVip());

        RespModel respModel = new RespModel();

        YxyLessonsTopicLessonItem item = itemRepository.findById(itemId);
        if (item != null) {

            String lessonId = item.getLessonId();
            String videoId = item.getVideoId();
            Integer orderNum = item.getOrderNum();

            respModel.itemId = itemId;
            respModel.title = item.getTitle();
            respModel.intro = item.getIntro();

            if (StringUtils.isNotBlank(videoId)) {
                YxyLessonsVideo video = videoRepository.findById(videoId);
                if (null != video) {
                    respModel.videoId = video.getId();
                    respModel.videoUrl = video.getLocation();
                    respModel.videoTitle = video.getFileName();
                    respModel.duration = disDuration(video.getDuration());
                    respModel.playStatus = video.getStatus() + "";
                    respModel.playUrl = YxyKeChengUtil.genLessonItemPlayUrl(itemId, userId, userVip);
                }
            }

            if (StringUtils.isNotBlank(lessonId)) {
                // 获取上一课时
                YxyLessonsTopicLessonItem upItem = itemRepository.findTop1ByLessonIdAndStatusNotAndOrderNumLessThanOrderByOrderNumDesc(lessonId, -1, orderNum);
                if (null != upItem) {
                    respModel.upItemId = upItem.getId();
                    respModel.upName = upItem.getTitle();
                }
                // 获取下一课时
                YxyLessonsTopicLessonItem downItem = itemRepository.findTop1ByLessonIdAndStatusNotAndOrderNumGreaterThanOrderByOrderNumAsc(lessonId, -1, orderNum);
                if (downItem != null) {
                    respModel.downItemId = downItem.getId();
                    respModel.downName = downItem.getTitle();
                }
            }
        }
        return respModel;
    }

    public static class ReqModel extends YxyApiReqModel {
        public String itemId;

        @Override
        public void autoCheck() {
            Assert.isTrue(StringUtils.isNotBlank(itemId), "课时不能为空");
        }
    }

    public static class RespModel extends YxyApiRespModel {
        public String itemId;
        public String title;
        public String intro;
        public String videoId;
        public String videoUrl;
        public String videoTitle;
        public String upItemId;
        public String upName;
        public String downItemId;
        public String downName;
        public String duration;
        public String playUrl;
        public String playStatus;
    }

    public String disDuration(Integer miao) {
        if (miao == null || miao < 0) {
            miao = 0;
        }
        int h = miao / 3600;
        int m = (miao % 3600) / 60;
        int s = (miao % 3600) % 60;
        if (h > 0) {
            return (h + "时" + m + "分" + s + "秒");
        } else {
            if (m > 0) {
                return (m + "分" + s + "秒");
            } else {
                return (s + "秒");
            }
        }
    }
}
